package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.modify.PhotoCompositionModel;
import dao.PhotoCompositionDao;
import mappings.PhotoComposition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("photos")
public class PhotoCompositionsService
{
    private PhotoCompositionDao photoCompositionDao = new PhotoCompositionDao();

    @Path("addPhoto")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhotoCompositionModel addNewCountry(@FormParam("description") String name, @FormParam("url") String url)
    {
        PhotoCompositionModel photoCompositionModel = new PhotoCompositionModel();

        try
        {
            PhotoComposition photo = photoCompositionDao.addPhoto(name, url);
            photoCompositionModel.setId(photo.getId());
            photoCompositionModel.setName(photo.getName());
            photoCompositionModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            photoCompositionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            photoCompositionModel.setStatus(2);
        }

        return photoCompositionModel;
    }

    @Path("editPhoto")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhotoCompositionModel editCountry(@FormParam("id") Integer id, @FormParam("description") String description, @FormParam("url") String url)
    {
        PhotoCompositionModel photoCompositionModel = new PhotoCompositionModel();

        try
        {
            PhotoComposition photo = photoCompositionDao.editPhoto(id, new PhotoComposition(description, url));
            photoCompositionModel.setName(photo.getName());
            photoCompositionModel.setId(photo.getId());
            photoCompositionModel.setUrl(photo.getUrl());
            photoCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            photoCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            photoCompositionModel.setStatus(2);
        }

        return photoCompositionModel;
    }

    @Path("deletePhoto")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public PhotoCompositionModel deletePhoto(@FormParam("id") Integer id)
    {
        PhotoCompositionModel photoCompositionModel = new PhotoCompositionModel();

        try
        {
            photoCompositionDao.deletePhoto(id);
            photoCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            photoCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            photoCompositionModel.setStatus(2);
        }

        return photoCompositionModel;
    }
}
